package com.cfk.shopmail.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.cfk.shopmail.product.entity.AttrEntity;
import com.cfk.shopmail.product.service.AttrAttrgroupRelationService;
import com.cfk.shopmail.product.service.AttrService;
import com.cfk.shopmail.product.service.CategoryService;
import com.cfk.shopmail.product.vo.AttrGroupRelationVo;
import com.cfk.shopmail.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.cfk.shopmail.product.entity.AttrGroupEntity;
import com.cfk.shopmail.product.service.AttrGroupService;
import com.cfk.common.utils.PageUtils;
import com.cfk.common.utils.R;



/**
 * 属性分组
 *
 * @author fengyaowen
 * @email 983050705@qq.com
 * @date 2021-11-12 22:27:02
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    // 获取分类下所有分组&关联属性
    // /product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        // 1、查出当前分类下所有属性的分组

        // 2、查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos =  attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", vos);

    }


    // 添加属性与分组关联关系
    // /product/attrgroup/attr/relation
    @RequestMapping("/attr/relation")
    public R attrRelation(@RequestBody List<AttrGroupRelationVo> vos) {
        attrAttrgroupRelationService.saveBatch(vos);
        return R.ok();
    }

    // 获取属性分组没有关联的其他属性
    // /product/attrgroup/{attrgroupId}/noattr/relation
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getAttrNoRelation(params, attrgroupId);
        return R.ok().put("page", page);
    }

    //删除关联关系
    //  /product/attrgroup/attr/relation/delete
    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {
        attrService.deleteRelation(vos);
        return R.ok();

    }

    /**
     * 根据分组id查找所有属性的名字
     */
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", entities);
    }

    /**
     * 列表
     * 按照对应三级分类进行分页查询
     */
    @RequestMapping("/list/{catelogId}")
        public R list(@RequestParam Map<String, Object> params,
                      @PathVariable("catelogId") Long catelogId){
        // PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPageByCatelogId(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     * 通过子id获取三级目录组
     */
    @RequestMapping("/info/{attrGroupId}")
        public R info(@PathVariable("attrGroupId") Long attrGroupId){

		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		//获取菜单的菜单目录id
        Long catelogId = attrGroup.getCatelogId();
        //返回目录路径数组
        Long[] categoryPath = categoryService.findCategoryPathById(catelogId);
        //存入当前菜单
        attrGroup.setCatelogPath(categoryPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
        public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
        public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
        public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
